package cn.lliiooll.ippzonetoolbox.ui.activities.input;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.lliiooll.ippzonetoolbox.R;
import cn.lliiooll.ippzonetoolbox.data.PData;
import cn.lliiooll.ippzonetoolbox.modules.nowm.result.IPostData;
import cn.lliiooll.ippzonetoolbox.task.PTaskBuilder;
import cn.lliiooll.ippzonetoolbox.utils.ActivitiesManager;
import cn.lliiooll.ippzonetoolbox.utils.IppzoneUtils;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.common.base.Strings;
import lombok.Getter;

@Route(path = "/ipp/input")
public class InputActivity extends AppCompatActivity {
    @Getter
    public static InputActivity instance;
    @Autowired
    public String type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        ARouter.getInstance().inject(this);
        setContentView(R.layout.activity_input);
        ActivitiesManager.add(this);
        final Button pass = findViewById(R.id.nowm_1_button);
        pass.setBackgroundResource(R.drawable.ic_button_24dp);
        pass.setAlpha(100);
        final EditText editText = findViewById(R.id.nowm_1_edittext);
        pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Strings.isNullOrEmpty(editText.getText().toString())) {
                    Toast.makeText(InputActivity.getInstance(), "帖子链接不能为空", Toast.LENGTH_SHORT).show();
                } else if (!editText.getText().toString().startsWith("http://share.ippzone.com/pp/post/") && !pass.getText().toString().startsWith("https://share.ippzone.com/pp/post/")) {
                    Toast.makeText(InputActivity.getInstance(), "请输入正确的帖子链接", Toast.LENGTH_SHORT).show();
                } else {
                    Toast t = Toast.makeText(InputActivity.getInstance(), "正在解析...", Toast.LENGTH_SHORT);
                    t.setDuration(1);
                    t.show();
                    if (type.equalsIgnoreCase("nowm")) {
                        final PData<String> data = new PData<>();
                        PTaskBuilder.builder()
                                .setLabel("nowm_jeixi")
                                .setRunnable(new Runnable() {
                                    @Override
                                    public void run() {
                                        data.setData(IppzoneUtils.noWmVideo(editText.getText().toString()));
                                    }
                                })
                                .onFinished(new Runnable() {
                                    @Override
                                    public void run() {
                                        ARouter.getInstance().build("/ipp/nowm").withString("url", data.getData()).navigation();
                                    }
                                })
                                .start();
                    } else if ((type.equalsIgnoreCase("find"))) {
                        final PData<String> data = new PData<>();
                        PTaskBuilder.builder()
                                .setLabel("find_data")
                                .setRunnable(new Runnable() {
                                    @Override
                                    public void run() {
                                        data.setData(IppzoneUtils.getIppzoneData(editText.getText().toString()).getData().getPost().getIp());
                                    }
                                })
                                .onFinished(new Runnable() {
                                    @Override
                                    public void run() {
                                        ARouter.getInstance().build("/ipp/find").withString("ip", data.getData()).navigation();
                                    }
                                })
                                .start();
                    } else {
                        Toast.makeText(InputActivity.getInstance(), "未知界面", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        final Button clear = findViewById(R.id.nowm_1_button_1);
        clear.setBackgroundResource(R.drawable.ic_button_24dp);
        clear.setAlpha(100);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = findViewById(R.id.nowm_1_edittext);
                editText.setText("");
            }
        });
    }

}
